   /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
/**
 *
 * @author MAN
 */
public class Empleado_DAO extends Model implements DaoGenerico {


    public boolean existe(Object o) throws SQLException{
             Integer id =(Integer)o;
             boolean sw=true;
		PreparedStatement pstmt=null;
		ResultSet rs=null;

		try{
			pstmt=getCon().prepareStatement("SELECT COUNT(*) FROM empleado WHERE codigo_empleado =?");
			pstmt.setInt(1, id.intValue());
			rs=pstmt.executeQuery();
			if(rs.next())
				if(rs.getString(1).equals("0"))
					sw=false;
		}finally{
			if(rs!=null)
				rs.close();

			if(pstmt!=null)
				pstmt.close();
		}
                 System.out.print("Valor sw:"+sw);
		return sw;
	}



      public Object load(ResultSet rs) throws SQLException {

        Empleado e = new Empleado();
        e.setCodigo(rs.getInt(1));
        e.setNombre(rs.getString(2));
        e.setNombre2(rs.getString(3));
        e.setApellido(rs.getString(4));
        e.setApellido2(rs.getString(5));
        e.setTelefono(rs.getString(6));
        e.setDireccion(rs.getString(7));
        e.setSalario(rs.getDouble(8));
        e.setTipoempleado(rs.getString(9));


        return e;
    }
      public Object extrae (Object  o) throws SQLException{
            Integer id =(Integer)o;

                    Connection conex=getCon();

                if (!isConnect())
			throw new SQLException("no hay conexcion");
                Empleado e= new Empleado();
		PreparedStatement pstmt=null;
		ResultSet rs=null;

		try{
			pstmt=conex.prepareStatement("SELECT * FROM empleado WHERE codigo_empleado=?");
			pstmt.setInt(1, id.intValue());
			rs=pstmt.executeQuery();
			if(rs.next()){
			  e=(Empleado)load(rs);

                        }else{

                        }
		}finally{
			if(rs!=null)
				rs.close();

			if(pstmt!=null)
				pstmt.close();
		}

		return e;
     }


        public void crear(Object o)throws SQLException{
         Empleado e=(Empleado)o;
             Connection conex=getCon();
                  Integer id= new Integer(e.getCodigo());
                if(existe(id))
                    throw new SQLException("Ya existe esta empleado");
		PreparedStatement pstmt=null;

		try{

			pstmt=conex.prepareStatement("insert into empleado values (?,?,?,?,?,?,?,?,?)");

			pstmt.setInt(1,e.getCodigo());
			pstmt.setString(2,e.getNombre());
                        pstmt.setString(3,e.getNombre2());
                        pstmt.setString(4,e.getApellido());
                        pstmt.setString(5,e.getApellido2());
                        pstmt.setString(6,e.getTelefono());
                        pstmt.setString(7,e.getDireccion());
		        pstmt.setDouble(8,e.getSalario());
			pstmt.setString(9,e.getTipoempleado());



			pstmt.executeUpdate();

		}finally{
			if(pstmt!=null){
				pstmt.close();
				pstmt=null;
			}
		}

  }
         public List extrae1(Object cod1) throws SQLException {
        Integer cod = (Integer) cod1;
        Connection conex = getCon();
        if (!isConnect()) {
            throw new SQLException("no hay conexcion");
        }

        PreparedStatement ps = null;
        ResultSet rs = null;
        Empleado e = null;
        List lista = new LinkedList();
        String sent = "select * from empleado where codigo_empleado like '%" +cod.intValue()+ "%'";
        try {
            ps = (PreparedStatement) conex.prepareStatement(sent);


            rs = ps.executeQuery();

                    while (rs.next()) {
                        e = (Empleado) load(rs);
                        lista.add(e);
                    }

        } finally {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
        }
        return lista;
    }


      public List extrae2(Object nom) throws SQLException {
        String  nom1 = (String) nom;
        Connection conex = getCon();
        if (!isConnect()) {
            throw new SQLException("no hay conexcion");
        }

        PreparedStatement ps = null;
        ResultSet rs = null;
        Empleado e = null;
        List lista = new LinkedList();
        String sent = "select * from empleado where nombre like '%"+nom1+ "%'";
        try {
            ps = (PreparedStatement) conex.prepareStatement(sent);


            rs = ps.executeQuery();


                    while (rs.next()) {
                        e = (Empleado) load(rs);
                        lista.add(e);
                    }


        } finally {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
        }
        return lista;
    }

        public List extrae3(Object nom) throws SQLException {
        String  nom1 = (String) nom;
        Connection conex = getCon();
        if (!isConnect()) {
            throw new SQLException("no hay conexcion");
        }

        PreparedStatement ps = null;
        ResultSet rs = null;
        Empleado e = null;
        List lista = new LinkedList();
        String sent = "select * from empleado where direccion like '%"+nom1+ "%'";
        try {
            ps = (PreparedStatement) conex.prepareStatement(sent);


            rs = ps.executeQuery();

                    while (rs.next()) {
                        e = (Empleado) load(rs);
                        lista.add(e);
                    }





        } finally {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
        }
        return lista;
    }





       public List listar() throws SQLException {
              Connection conex=getCon();
        if (!isConnect())
		throw new SQLException("no hay conexcion");

        PreparedStatement ps = null;
        ResultSet rs = null;
        Empleado e= null;
        List lista = new LinkedList();
        try {
            ps =(PreparedStatement)  conex.prepareStatement("select * from empleado ");
            rs = ps.executeQuery();

            while (rs.next()) {
                e=(Empleado) load(rs);
                lista.add(e);
            }

        } finally {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
        }
        return lista;
    }

        public  boolean update (Empleado e) throws SQLException {
              Connection conex=getCon();
                                Integer id= new Integer(e.getCodigo());

       if (!existe(id)) {
           throw new SQLException ("Empleado no registrado "+e.getCodigo());
       }
       PreparedStatement pstmt = null;
       boolean res = false;
       try {
         pstmt = conex.prepareStatement("Update empleado set codigo_empleado  = ?, nombre = ?, segundonombre=?, apellido=?, segundoapellido=?, telefono=?, direccion=? salario  = ?, tipo = ?  where codigo_empleado = ?");
        	        pstmt.setInt(1,e.getCodigo());
			pstmt.setString(2,e.getNombre());
                        pstmt.setString(3,e.getNombre2());
                        pstmt.setString(4,e.getApellido());
                        pstmt.setString(5,e.getApellido2());
                        pstmt.setString(6,e.getTelefono());
                        pstmt.setString(7,e.getDireccion());
		        pstmt.setDouble(8,e.getSalario());
			pstmt.setString(9,e.getTipoempleado());

      
         int r = pstmt.executeUpdate();
       if (r != 0) {
          res = true;
       }
       } finally {
          if (pstmt != null) {
            pstmt.close();
          }
       }
         return res;
 }

    public List listar(Object o) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List listarlista(List l) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Cliente find(Integer id) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }


}
